Completed
Pull Request — develop (#211)
by Xaver
33s
created

main.js ➔ ... ➔ hasLanguage   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 9.4285
1
define(['moment', 'utils/router', 'leaflet', 'gui', 'helper', 'utils/language'],
2
  function (moment, Router, L, GUI, helper, Language) {
3
    'use strict';
4
5
    return function () {
6
      function handleData(data) {
7
        var timestamp;
8
        var nodes = [];
9
        var links = [];
10
        var nodeDict = {};
11
12
        for (var i = 0; i < data.length; ++i) {
13
          nodes = nodes.concat(data[i].nodes);
14
          timestamp = data[i].timestamp;
15
          links = links.concat(data[i].links);
16
        }
17
18
        nodes.forEach(function (node) {
19
          node.firstseen = moment.utc(node.firstseen).local();
20
          node.lastseen = moment.utc(node.lastseen).local();
21
        });
22
23
        var now = moment();
24
        var age = moment(now).subtract(config.maxAge, 'days');
25
26
        var online = nodes.filter(function (d) {
27
          return d.is_online;
28
        });
29
        var offline = nodes.filter(function (d) {
30
          return !d.is_online;
31
        });
32
33
        var newnodes = helper.limit('firstseen', age, helper.sortByKey('firstseen', online));
34
        var lostnodes = helper.limit('lastseen', age, helper.sortByKey('lastseen', offline));
35
36
        nodes.forEach(function (d) {
37
          d.neighbours = [];
38
          nodeDict[d.node_id] = d;
39
        });
40
41
        links.forEach(function (d) {
42
          d.source = nodeDict[d.source];
43
          d.target = nodeDict[d.target];
44
45
          d.id = [d.source.node_id, d.target.node_id].join('-');
46
          d.source.neighbours.push({ node: d.target, link: d });
47
          d.target.neighbours.push({ node: d.source, link: d });
48
49
          try {
50
            d.latlngs = [];
51
            d.latlngs.push(L.latLng(d.source.location.latitude, d.source.location.longitude));
52
            d.latlngs.push(L.latLng(d.target.location.latitude, d.target.location.longitude));
53
54
            d.distance = d.latlngs[0].distanceTo(d.latlngs[1]);
55
          } catch (e) {
56
            // ignore exception
57
          }
58
        });
59
60
        return {
61
          now: now,
62
          timestamp: moment.utc(timestamp).local(),
63
          nodes: {
64
            all: nodes,
65
            online: online,
66
            offline: offline,
67
            new: newnodes,
68
            lost: lostnodes
69
          },
70
          links: links,
71
          nodeDict: nodeDict
72
        };
73
      }
74
75
      var language = new Language();
76
      window.router = new Router(language);
77
78
      config.dataPath.forEach(function (d, i) {
79
        config.dataPath[i] += 'meshviewer.json';
80
      });
81
82
      language.init(router);
83
84
      function update() {
85
        return Promise.all(config.dataPath.map(helper.getJSON))
86
          .then(handleData);
87
      }
88
89
      update()
90
        .then(function (a) {
91
          function hasLanguage() {
92
            console.log(_.phrases.yes);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
93
            if (_.phrases.yes === undefined) {
94
              console.log('b');
95
              window.setTimeout(hasLanguage, 20);
96
              return false;
97
            }
98
            console.log('a');
99
            return a;
100
          }
101
          return hasLanguage();
102
        })
103
        .then(function (d) {
104
          var gui = new GUI(language);
105
          gui.setData(d);
106
          router.setData(d);
107
          router.resolve();
108
109
          window.setInterval(function () {
110
            update().then(function (n) {
111
              gui.setData(n);
112
              router.setData(n);
113
            });
114
          }, 60000);
115
        })
116
        .catch(function (e) {
117
          document.querySelector('.loader').innerHTML += e.message
118
            + '<br /><br /><button onclick="location.reload(true)" class="btn text" aria-label="Try to reload">Try to reload</button><br /> or report to your community';
119
          console.warn(e);
120
        });
121
    };
122
  });
123